﻿document.write('<link href="/js/layui/css/layui.css" rel="stylesheet" type="text/css" />');
document.write('<script src="/js/jquery.js" type="text/javascript"></script>');
document.write('<script src="/js/layui/layui.js" type="text/javascript"></script>');
document.write('<script src="/Apm/js/limit.js" type="text/javascript"></script>');

document.write('<link href="/css/CMSEdit.css" rel="stylesheet" type="text/css" />');

/**
 * 获取图片数据
 * @param AnnexMaster 对应主表guid
 * @param AnnexModule 对应模块
 */
function GetBindImage(AnnexMaster, AnnexModule) {
    var CompanyCode = MyPublic.getCompanyCode();
    var json = new Object();
    json.AnnexMaster = AnnexMaster;
    json.AnnexModule = AnnexModule;
    json.CompanyCode = CompanyCode;
    $.ajax({
        url: ApiURL + 'api/GetFile/GetImage',
        type: 'post',
        contentType: 'application/json',
        data: JSON.stringify(json),
        success: function (data) {

            if (data != null) {
                var htmlArray = [];
                var count = data.length;

                for (var i = 0; i < count; i++) {
                    htmlArray.push('<div id="' + data[i].AnnexCode + '"  class="imgrow"><img id="view' + data[i].AnnexCode + '" data-url="' + data[i].AnnexUrl + data[i].AnnexNewName + '"  data-thumb="' + data[i].ThumbnailUrl + '"  data-name="' + data[i].AnnexOldName + '" onclick="ViewImage(this.id)"  src="' + ApiURL + data[i].ThumbnailUrl + '"><div onclick="deleteupload(\'' + data[i].AnnexCode + '\')" class="delete-img">×</div></div>');
                }
                var html = htmlArray.join("");
                $("#uploadimg").before(html);
            }

        },
        error: function () {
            location.href = "/error.html";
        }
    });
}
/**
 * 删除附件
 * @param id
 */
function deleteupload(id) {

    $.ajax({
        url: ApiURL + 'api/Method/FileDelete?token=' + MyPublic.getToken(),
        type: 'get',
        contentType: 'application/json',
        data: { annxecode: id },
        success: function () {
            $("#" + id).empty();
        },
        error: function () {
            location.href = "/error.html";

        }
    })
}
/**
 * 查看大图
 * @param id
 */
function ViewImage(id) {
    var name = $('#' + id).attr("data-name");
    var imgurl = $('#' + id).attr("data-url");
    var thumb = $("#" + id).attr("data-thumb");
    layui.use('layer', function () {
        var layer = layui.layer;

        var json = {
            "title": "相册", //相册标题
            "id": "view", //相册id
            "start": 0, //初始显示的图片序号，默认0
            "data": [   //相册包含的图片，数组格式
                {
                    "alt": "" + name + "",
                    "pid": 1, //图片id
                    "src": "" + ApiURL + imgurl + "", //原图地址
                    "thumb": "" + ApiURL + thumb + "" //缩略图地址
                }
            ]
        };
        layer.photos({
            photos: json
            , anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
        });

    })
}